using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParticleMeshSimple : MonoBehaviour
{
    ParticleSystem m_Particle;
    ParticleSystemRenderer _renderer;
    float time = 0;
    Mesh workMesh;

    MeshFilter m_MeshFilter;
    public void Set(ParticleSystem particle, MeshFilter meshFilter)
    {
        m_Particle = particle;
        m_Particle.TryGetComponent(out _renderer);
        m_MeshFilter = meshFilter;

        m_Particle.Clear();
        m_Particle.Pause();

        time = 0;
    }

    // Update is called once per frame
    void Update()
    {
        if (m_Particle == null) { return; }
        time += Time.deltaTime;
        m_Particle.Simulate(time);

        if (workMesh == null) { workMesh = new Mesh(); }
        _renderer.BakeMesh(workMesh);
        m_MeshFilter.mesh = workMesh;
    }
}
